package cn.dengta.webapp.trade.dao;

import java.util.List;

import cn.dengta.common.model.TimeInterval;
import cn.dengta.webapp.base.dao.BaseDao;
import cn.dengta.webapp.trade.entity.FemaleRecommendStatistics;
import cn.dengta.webapp.trade.entity.UserCallStatistics;
import cn.dengta.webapp.user.entity.User;
import org.apache.ibatis.annotations.*;

/**
 * 女用户推荐分值统计 Mapper 接口
 *
 * @author zw
 * @since 2022-10-09
 */
public interface FemaleRecommendStatisticsDao extends BaseDao<FemaleRecommendStatistics> {

	/** 更新某个字段值 */
	@Update("UPDATE dt_female_recommend_statistics SET ${field} = #{score} WHERE id = #{userId}")
	int coverFieldValue(@Param("field") String field, @Param("score") Integer score, @Param("userId") Long userId);

	@Update("UPDATE dt_female_recommend_statistics SET ${field} = ${field} + #{score} WHERE id = #{userId}")
	int addFieldValue(@Param("field") String field, @Param("score") Integer score, @Param("userId") Long userId);

	@Update("UPDATE dt_female_recommend_statistics SET ${field} = #{score} WHERE id = #{userId}")
	int coverTotleAndFieldValue(@Param("field") String field, @Param("score") Integer score, @Param("userId") Long userId);

	@Select("SELECT u.id, u.info_supply_flag, u.avatar_status FROM dt_female_recommend_statistics famale JOIN dt_user u ON famale.id = u.id WHERE famale.id > #{maxId} ORDER BY famale.id LIMIT #{limit}")
	List<User> selectUserInfoSupply(@Param("maxId") long maxId, @Param("limit") int limit);

	@Update("UPDATE dt_female_recommend_statistics SET ${field} = 0")
	void resetFieldScore(@Param("field") String field);

	@Update("UPDATE dt_user u SET u.sort=(SELECT SUM(info+reply_rate+avatar+video_rate+audio_rate+login) FROM dt_female_recommend_statistics WHERE id = u.id) WHERE gender = 0")
	void updateTotalSort();

	@Select("SELECT s.user_id,SUM(audio_get) audioGet,SUM(audio_get_accepted) audioGetAccepted,SUM(video_get) videoGet,SUM(video_get_accepted) videoGetAccepted FROM dt_user_call_statistics s JOIN dt_user u ON s.user_id = u.id " +
			"WHERE add_date BETWEEN #{interval.finalBegin} AND #{interval.finalEnd} AND u.gender = 0 GROUP BY s.user_id")
	List<UserCallStatistics> callRate(@Param("interval") TimeInterval addTime);

}
